const mysql = require('mysql2');
const dbConfig = require('./dbconfig');

const pool = mysql.createPool({
    host: dbConfig.MYSQL.HOST,
    user: dbConfig.MYSQL.USERNAME,
    password: dbConfig.MYSQL.PASSWORD,
    database: dbConfig.MYSQL.DATABASE,
    multipleStatements: true, // 允许多条sql查询
    dateStrings: true // 日期字段保存格式为字符串，不做时区转换
});

class MysqlPool {
    static query(sql, values) {
        return new Promise((resolve, reject) => {
            pool.getConnection(function (err, connection) {
                if (err) {
                    reject(err)
                } else {
                    connection.query(sql, values, (err, rows) => {
                        if (err) {
                            reject(err)
                        } else {
                            resolve(rows)
                        }
                        connection.release()
                    })
                }
            })
        })
    }
}

module.exports = MysqlPool;